#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
typedef long long ll;
using namespace std;
ll arry[1010];
ll jg[1010];
int main(){
    int n,ml;
    while (cin>>n){
        while (n--){
           cin>>ml;
           memset(arry,0, sizeof(arry));
           memset(jg,0, sizeof(jg));
           int sflag=1,qflag=1;
            for (int i = 0; i < ml; ++i) {
                cin>>arry[i];
            }
            for (int i = 0; i < ml; ++i) {
                cin>>jg[i];
            }
            for (int i = 0; i < ml; ++i) {
                if (jg[i]!=arry[i]){
                    qflag=0;
                    break;
                }
            }
            for (int i = 0,j = ml-1; i < ml; ++i,--j) {
                if (jg[j]!=arry[i]){
                    sflag=0;
                    break;
                }
            }
            if (qflag&&sflag){
                cout<<"both"<<endl;
            } else if (qflag){
                cout<<"queue"<<endl;
            } else if (sflag){
                cout<<"stack"<<endl;
            } else{
                cout<<"neither"<<endl;
            }
            
        }
    }
    return 0;
}